<?php
/**
 * Created by PhpStorm.
 * Author: huangwy
 * Date: 2018/8/7 16:13
 * Version: 1.0
 */

namespace app\admin\validate;

use think\Validate;

/**
 * Class Article
 * @package app\admin\validate
 */
class Article extends Validate
{
    //验证规则
    protected $rule = [
        'title'     => 'require|checkEmpty',
        'sid'    => 'require|checkEmpty',
        'content'   => 'checkContent',
        'link'      => 'url'
    ];
    
    //错误消息
    protected $message = [
        'title'    => '标题不能为空',
        'content.checkContent'  => '内容不能为空',
        'sid.require'   => '所属分类缺少参数',
        'sid.checkEmpty' => '所属分类必须选择',
        'article_id.checkArtcileId' => '系统预定义的文章不能删除',
        'link.url' => '链接格式错误'
    ];
    
    //验证场景
    protected $scene = [
        'add'  => ['title', 'sid', 'content'],
        'edit' => ['title', 'sid', 'content'],
        'del'  => ['sid']
    ];
    
    protected function checkEmpty($value)
    {
        if (is_string($value)) {
            $value = trim($value);
        }
        if (empty($value)) {
            return false;
        }
        return true;
    }
    
    protected function checkContent($value,$rule,$data)
    {
        $value = strip_tags($value);
        $value = str_replace('&nbsp;', '', $value);
        $value = trim($value);
        if(empty($data['link']) && empty($value)) {
             return false;
        }
        return true;
    }

}
